package com.project.controller.web;

import com.alibaba.fastjson.JSONObject;
import com.project.common.utils.MyPageHelper;
import com.project.entity.*;
import com.project.myBeans.MagfacultyInfoDetail;
import com.project.myBeans.R;
import com.project.myBeans.ResponseBean;
import com.project.service.*;
import com.project.service.base.BaseService;
import com.project.service.base.CrpData.CrypDataHelper;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/27 12:49
 * @param:
 * @return:
 */
@EnableAutoConfiguration //自动载入应用程序所需的所有Bean
@RestController //@Controller与@ResponseBody的合并
@RequestMapping(value = "/magfacultyinfo")
@Api(description = "应聘控制",value = "magfacultyinfo")
public class MagfacultyInfoController extends BaseService{
    @Autowired
    private MagFacultyInfoService magFacultyInfoService;
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private UniversityService universityService;
    @Autowired
    private UserService userService;
    @Autowired
    private MagFacultyService magFacultyService;
    @Autowired
    private CrypDataHelper crypDataHelper;

    /**
     * TateBrown 2018年5月27日
     *
     * @param userId
     * @return
     */
    @ApiOperation("按userId查找行政人员信息")
    @GetMapping("/findOne/{userId}")
    public R findOne(@PathVariable("userId") Integer userId) {
        JSONObject jsonObject = new JSONObject();
        User user;
        School school = null;
        Department department = null;
        University university = null;
        MagFacultyInfo magFacultyInfo;
        try {
            user = userService.findOneByUserID(userId);
            jsonObject.put("user", user);

            magFacultyInfo = magFacultyInfoService.findByUserId(userId);
            if (magFacultyInfo != null) {
                if (magFacultyInfo.getDivisionid() != null)
                    department = departmentService.findOne(magFacultyInfo.getDivisionid());
                if (magFacultyInfo.getSchoolid() != null)
                    school = schoolService.findOne(magFacultyInfo.getSchoolid());
                if (school != null) {
                    if (school.getUniversityid() != null)
                        university = universityService.findOne(school.getUniversityid());
                }
                jsonObject.put("magFacultyInfo", magFacultyInfo);
                jsonObject.put("university", university);
                jsonObject.put("school", school);
                jsonObject.put("department", department);
            }

            return R.ok("查找成功").put("data",jsonObject);
        } catch (Exception e) {
            return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param magfacultyInfoDetail
     * @return
     */
    @ApiOperation("根据id修改行政人员相关信息（用户表 + 行政人员信息表）")
    @PostMapping("/modifyOne")
    public R modifyOne(@RequestBody MagfacultyInfoDetail magfacultyInfoDetail) {
        try {
            User user = magfacultyInfoDetail.getUser();
            MagFacultyInfo magFacultyInfo = magfacultyInfoDetail.getMagFacultyInfo();
            if (user != null) {
                userService.updateUser(user);
                if (magFacultyInfo != null)
                    magFacultyInfoService.modifyByUserId(magFacultyInfo);
               return R.ok("修改成功");
            } else {
              return R.error("无信息可以修改");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param magfacultyInfoDetail
     * @return
     */
    @ApiOperation("创建行政人员相关信息（用户表 + 行政人员信息表）")
    @PostMapping("/addOne")
    public R addOne(@RequestBody MagfacultyInfoDetail magfacultyInfoDetail) {
        try {
            User user = magfacultyInfoDetail.getUser();
            MagFacultyInfo magFacultyInfo = magfacultyInfoDetail.getMagFacultyInfo();
            if (user != null) {
                if (magFacultyInfo != null) {
                    magFacultyService.addOne(magfacultyInfoDetail);
                  return R.ok("创建成功");
                } else {
                    return R.error("缺少行政人员信息表");
                }
            } else {

               return R.error("缺少用户表信息");
            }

        } catch (Exception e) {
            return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param userId
     * @return
     */
    @ApiOperation("根据用户id删除行政人员账号")
    @GetMapping("/removeOneByUserId/{userId}")
    public R removeOneByUserId(@PathVariable("userId") Integer userId) {
        try {
            if (magFacultyService.removeOneByUserId(userId)) {
               return R.ok("删除成功");
            } else {
               return R.error("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
          return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param magfacultyInfoDetail
     * @return
     */
    @ApiOperation("根据用户表、行政人员信息表筛选")
    @PostMapping("/findBatchByCondition")
    public R findBatchByCondition(@RequestBody MagfacultyInfoDetail magfacultyInfoDetail) {
        try {
            List<JSONObject> jsonObjectList = new ArrayList<>();
            magfacultyInfoDetail.setPageIndex(magfacultyInfoDetail.getPageIndex() - 1);
            List<MagfacultyInfoDetail> magfacultyInfoDetailList = magFacultyInfoService.findBatchByCondition(magfacultyInfoDetail);
            for (MagfacultyInfoDetail magfacultyInfoDetail1 : magfacultyInfoDetailList) {
                JSONObject jsonObject = new JSONObject();
                Department department;
                User user = magfacultyInfoDetail1.getUser();
                MagFacultyInfo magFacultyInfo = magfacultyInfoDetail1.getMagFacultyInfo();
                //解密用户姓名
                if (user != null) {
                    if (user.getRealname() != null)
                        user.setRealname(crypDataHelper.decRealName(user.getRealname()));
                    //过滤敏感信息
                    user.setPassword(null);
                    user.setSalt(null);
                    magfacultyInfoDetail.setUser(user);
                }
                jsonObject.put("magfacultyInfoDetail", magfacultyInfoDetail1);
                //获取系信息
                if (magFacultyInfo != null)
                    if (magFacultyInfo.getDivisionid() != null) {
                        department = departmentService.findOne(magFacultyInfo.getDivisionid());
                        jsonObject.put("department", department);
                    }
                jsonObjectList.add(jsonObject);
            }
            return R.ok("查找成功").put("data",jsonObjectList).put("total", MyPageHelper.getTotal(jsonObjectList));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
}
